/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actor.weapons;

import Actor.Actor;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import javax.vecmath.Point2d;
import projectshanghai.World;

/**
 *
 * @author Eric-Standard
 */
public abstract class Weapon extends Actor {

    protected int cooldownTimeout = 100;
    protected int cooldownTime = 0;
    protected int reloadTimeout = 1000;
    protected int reloadTime = 0;
    protected int damage;
    protected int ammo, totalAmmo, maxAmmo, clipSize, reloadAmt;
    protected boolean firing = false;
    protected boolean reloading = false;
    //An automatic weapon will fire as long as the mouse is held down
    protected boolean automatic = false;
    protected Actor owner;

    public Weapon() {
        super();
    }

    public Weapon(Actor a) {
        super();
        owner = a;
        this.world = owner.getWorld();
    }

    @Override
    public void act(long dt) {
        this.angle = owner.getAngle();
        this.pos = (Point2d) owner.getPos().clone();
        this.pos.x += 5 * Math.cos(owner.getAngle());
        this.pos.y += 5 * Math.sin(owner.getAngle());
        //Update cooldown and reload times
        if (firing) {
            cooldownTime -= dt;
            if (cooldownTime < 0) {
                cooldownTime = 0;
                firing = false;
            }
        }
        if (reloading) {
            reloadTime -= dt;
            if (reloadTime < 0) {
                reloadTime = 0;
                //Calculate reload amount
                reloadAmt = clipSize - ammo;
                reloadAmt = Math.min(totalAmmo, reloadAmt);
                ammo += reloadAmt;
                totalAmmo -= reloadAmt;
                reloading = false;
            }
        }
    }

    public boolean fire() {
        if (firing || ammo == 0 || reloading) {
            return false;
        }
        ammo--;
        cooldownTime = cooldownTimeout;
        firing = true;
        return true;
    }

    public void reload() {
        if (reloading) {
            return;
        }
        reloadAmt = clipSize - ammo;
        reloadAmt = Math.min(totalAmmo, reloadAmt);
        if (reloadAmt != 0) {
            reloadTime = reloadTimeout;
            reloading = true;
        }
    }

    public int getAmmoInClip() {
        return ammo;
    }

    public int getTotalAmmo() {
        return totalAmmo;
    }

    public void addAmmo(int ammo) {
        totalAmmo += ammo;
    }

    @Override
    public Rectangle2D getBoundingRectangle() {
        return null;
    }

    public void setOwner(Actor owner) {
        this.owner = owner;
        this.world = owner.getWorld();
    }

    public Actor getOwner() {
        return owner;
    }

    public void setDamage(int damage) {
        this.damage = damage;
    }

    public int getDamage() {
        return damage;
    }

    @Override
    public Area getArea() {
        return null;
    }

    /**
     * Draws the weapon info on the HUD.
     * @param g 
     */
    public void drawHUD(Graphics g) {
        g.setColor(Color.BLUE);
        Dimension size = world.getSize();
        int ammoTextX = size.width - World.HUD_WIDTH + 1;
        int ammoTextY = size.height - World.HUD_HEIGHT + 1;
        g.drawString(Integer.toString(getAmmoInClip()), ammoTextX, ammoTextY);
        g.drawString("|", ammoTextX + 16, ammoTextY - 1);
        g.drawString(Integer.toString(getTotalAmmo()), ammoTextX + 20, ammoTextY);
    }
}
